﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoanPrequalificationSystem.Model.Borrowers
{
    public class CreditScore
    {
        private int _score;
        private string _creditAgency;

        public CreditScore()
        {

        }

        public CreditScore(string creditAgency, int Score)
        {
            _creditAgency = creditAgency;
            _score = Score;

            if (GetBrokenRules().Count > 0)
            {
                throw new Exception(string.Format("The Credit score has been created with invalid data. {0}", GetBrokenRulesToString()));

            }

        }
        public string CreditAgency
        {
            get
            {
                return _creditAgency;
            }
            set
            {
                _creditAgency = value;
            }
        }

        public int Score
        {
            get
            {
                return _score;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} : Score {1}", _creditAgency, _score);
        }
        public List<BrokenBusinessRule> GetBrokenRules()
        {
            var brokenRules = new List<BrokenBusinessRule>();

            if (string.IsNullOrEmpty(CreditAgency))
            {
                brokenRules.Add(new BrokenBusinessRule("CreditAgency", "You must enter a valid credit agency for this credit score."));
            }
            if (Score < 400 || Score > 900)
            {
                brokenRules.Add(new BrokenBusinessRule("Score", "You must enter a valid score between 400 and 900."));
            }
            return brokenRules;
        }

        private string GetBrokenRulesToString()
        {
            var sbBrokenRules = new StringBuilder();
            foreach (var brokenRule in GetBrokenRules())
            {
                sbBrokenRules.Append(brokenRule.Rule);
            }
            return sbBrokenRules.ToString();
        }

    }
}
